package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JToolBar;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import controllers.ClientController;

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.ImageIcon;

import entity.UserLoginInfo;

public class SubscribingByCustomer extends JFrame {

	ClientController clientCon;
    static String ID;
    private JPanel contentPane;
	private JTable table;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SubscribingByCustomer frame = new SubscribingByCustomer(ID);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Subscribing By Customer frame.
	 */
	public SubscribingByCustomer(String id) {
		this.ID=id;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setForeground(new Color(0, 0, 0));
		contentPane.setBorder(new TitledBorder(null, "Subscribing Window", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 255, 51)));
		setContentPane(contentPane);
		
		
		final JComboBox selectionBox = new JComboBox();		
		selectionBox.setBounds(208, 181, 234, 20);
		selectionBox.setModel(new DefaultComboBoxModel(new String[] {"", "Package", "Subscribtion Information Layer", "Member Location Time"}));			
		selectionBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
				
		
		
		JButton btnConfirm = new JButton("Confirm");		//confirm button ******************************************//
		btnConfirm.setBounds(208, 250, 85, 23);
		btnConfirm.addActionListener(new ActionListener() {
			private String selection;
			private int index;
			public void actionPerformed(ActionEvent e) {
				try{
					this.selection=selectionBox.getSelectedItem().toString();
					this.index= (int)selectionBox.getSelectedIndex();
					CustomerGui frame = new CustomerGui(ID);
					frame.setVisible(true);
					dispose();
					btnConfirmActionPerformed(e,selection,index);
				}catch(IOException e1) {
					e1.printStackTrace();
				}
			
			}
		});
		
		JButton btnCancel = new JButton("Back");
		btnCancel.setBounds(311, 250, 85, 23);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CustomerGui frame = new CustomerGui(ID);
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnCan = new JButton("Cancel");
		btnCan.setBounds(406, 250, 85, 23);
		btnCan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JLabel lblPleaseSelect = new JLabel("Please Select Subscriber Type : ");
		lblPleaseSelect.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPleaseSelect.setBounds(209, 156, 268, 14);
		
		JLabel lblWelcomToSubscribing = new JLabel("Welcome To Subscribing Window");
		lblWelcomToSubscribing.setBounds(125, 16, 334, 22);
		lblWelcomToSubscribing.setFont(new Font("Snap ITC", Font.ITALIC, 16));
		lblWelcomToSubscribing.setBackground(new Color(51, 102, 51));
		lblWelcomToSubscribing.setToolTipText("");
		
		table = new JTable();
		table.setBounds(16, 64, 581, 67);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"Package", "Subscription information layer", "Member Location Time"},
				{"200 purchases per montg whit 30% discount ", "cost of 50$ per month per layer", " Receiving a picture every day at a cost of 200$"},
			},
			new String[] {
				"Package", "New column", "New column"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(209);
		table.getColumnModel().getColumn(1).setPreferredWidth(210);
		table.getColumnModel().getColumn(1).setMinWidth(53);
		table.getColumnModel().getColumn(2).setPreferredWidth(226);
		table.setColumnSelectionAllowed(true);
		table.setCellSelectionEnabled(true);
		contentPane.setLayout(null);
		contentPane.add(lblWelcomToSubscribing);
		contentPane.add(lblPleaseSelect);
		contentPane.add(table);
		contentPane.add(btnConfirm);
		contentPane.add(btnCancel);
		contentPane.add(btnCan);
		contentPane.add(selectionBox);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//spiral-rainbow.jpg"));
		lblNewLabel.setBounds(0, 0, 613, 347);
		contentPane.add(lblNewLabel);
		lblNewLabel.setLayout(null);
	}
	public JTable getTable() {
		return table;
	}
	/**
	 * connect with server ,handleMessageFromGui
	 * @param e
	 * @param selc
	 * @param ind
	 * @throws IOException
	 */
	
	private void btnConfirmActionPerformed(ActionEvent e,String selc,int ind) throws IOException {
		
		String choice = selc;
		UserLoginInfo logi=new UserLoginInfo();
		String ip=logi.getIP();
		int port=5555;
		int temp=ind;
		ArrayList<String> arr=new ArrayList<String>();
		System.out.println("subscribingByCustomerGUI " +choice+ "  " +temp);
		//this.selection="Package";

		
		if(choice.equals(""))
		{
			JOptionPane.showMessageDialog(null, "No Choice Made ");
		}
		else{
			if(choice.equals("Package")){
				choice="Package";
			}
			else if(choice.equals("Subscription information layer")){
				choice="Subscription information layer";
			}
			else if(choice.equals("Member Location Time")){
				choice="Member Location Time";
			}
		
			arr.add("subscribingByCustomer");
			arr.add(this.ID);
			arr.add(choice);
			this.clientCon=new ClientController(ip,port);		
			clientCon.handleMessageFromGui(arr);
			
		}
	}
}
